# VSCode插件自动安装和配置脚本
# 运行方式：在项目根目录打开PowerShell，执行 .\install-plugins.ps1

Write-Host "🚀 开始配置VSCode插件..." -ForegroundColor Green

# 检查VSCode是否安装
$codeCommand = Get-Command code -ErrorAction SilentlyContinue
if (-not $codeCommand) {
    Write-Host "❌ 未找到VSCode命令行工具。请确保VSCode已安装并添加到PATH。" -ForegroundColor Red
    Write-Host "解决方案：在VSCode中按Ctrl+Shift+P，输入'Shell Command: Install code command in PATH'" -ForegroundColor Yellow
    exit 1
}

Write-Host "✅ 找到VSCode命令行工具" -ForegroundColor Green

# 首先卸载冲突的插件
Write-Host "🗑️ 正在卸载冲突的插件..." -ForegroundColor Yellow

$conflictingExtensions = @(
    "octref.vetur",
    "typescript-importer.typescript-importer", 
    "rvest.vs-code-prettier-eslint",
    "hookyqr.beautify",
    "ms-vscode.brackets-pack",
    "formulahendry.code-runner"
)

foreach ($extension in $conflictingExtensions) {
    Write-Host "禁用插件: $extension" -ForegroundColor Yellow
    & code --uninstall-extension $extension 2>$null
}

# 安装必需的插件
Write-Host "📦 正在安装必需的插件..." -ForegroundColor Green

$requiredExtensions = @(
    "Vue.volar",                                    # Vue Language Features (Volar)
    "Vue.vscode-typescript-vue-plugin",            # TypeScript Vue Plugin (Volar)
    "dbaeumer.vscode-eslint",                       # ESLint
    "esbenp.prettier-vscode"                        # Prettier
)

foreach ($extension in $requiredExtensions) {
    Write-Host "安装插件: $extension" -ForegroundColor Green
    & code --install-extension $extension --force
    if ($LASTEXITCODE -eq 0) {
        Write-Host "✅ 成功安装: $extension" -ForegroundColor Green
    } else {
        Write-Host "❌ 安装失败: $extension" -ForegroundColor Red
    }
}

# 安装推荐的插件
Write-Host "🔧 正在安装推荐的插件..." -ForegroundColor Cyan

$recommendedExtensions = @(
    "bradlc.vscode-tailwindcss",                   # Tailwind CSS IntelliSense
    "christian-kohler.path-intellisense",          # Path Intellisense
    "formulahendry.auto-rename-tag",               # Auto Rename Tag
    "usernamehw.errorlens"                         # Error Lens
)

foreach ($extension in $recommendedExtensions) {
    Write-Host "安装推荐插件: $extension" -ForegroundColor Cyan
    & code --install-extension $extension --force
    if ($LASTEXITCODE -eq 0) {
        Write-Host "✅ 成功安装: $extension" -ForegroundColor Green
    } else {
        Write-Host "❌ 安装失败: $extension" -ForegroundColor Red
    }
}

# 安装可选的插件
Write-Host "🎨 正在安装可选的插件..." -ForegroundColor Magenta

$optionalExtensions = @(
    "eamodio.gitlens",                             # GitLens
    "mhutchie.git-graph",                          # Git Graph  
    "PKief.material-icon-theme",                   # Material Icon Theme
    "zhuangtongfa.material-theme"                  # One Dark Pro Theme
)

foreach ($extension in $optionalExtensions) {
    Write-Host "安装可选插件: $extension" -ForegroundColor Magenta
    & code --install-extension $extension --force
    if ($LASTEXITCODE -eq 0) {
        Write-Host "✅ 成功安装: $extension" -ForegroundColor Green
    } else {
        Write-Host "❌ 安装失败: $extension" -ForegroundColor Red
    }
}

Write-Host ""
Write-Host "🎉 插件安装完成！" -ForegroundColor Green
Write-Host ""
Write-Host "📋 接下来的步骤：" -ForegroundColor Yellow
Write-Host "1. 重启VSCode (完全关闭后重新打开)" -ForegroundColor White
Write-Host "2. 打开项目中的任意.vue文件" -ForegroundColor White  
Write-Host "3. 检查右下角状态栏是否显示'Vue'" -ForegroundColor White
Write-Host "4. 测试语法高亮是否正常" -ForegroundColor White
Write-Host ""
Write-Host "🔧 如果仍有问题，请查看 VSCode插件诊断与修复.md 文件" -ForegroundColor Cyan

# 检查已安装的插件
Write-Host ""
Write-Host "📋 当前已安装的Vue相关插件：" -ForegroundColor Blue
& code --list-extensions | Where-Object { $_ -match "(vue|volar|prettier|eslint)" } | ForEach-Object {
    Write-Host "✅ $_" -ForegroundColor Green
}

Write-Host ""
Write-Host "🚨 如果发现Vetur插件仍然存在，请手动禁用它！" -ForegroundColor Red 